//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 
    uid: "",
    // 
    preMoneyArr: [],
    maFeeStr: "",
    moneyStr: "",
    // 
    preMoney: "",
    rewardMoney: "",
    comeMaFee: "",
    // 
    preInputFlag: true,
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",
    // 
    payFlag: true,
    orderNumber: "",
    payType: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
    })
    this.GetBusinessPrestoreQuotaAndReward();
  },

  GetBusinessPrestoreQuotaAndReward: function() {
    var that = this;
    var uid = this.data.uid;
    var preMoneyArr = this.data.preMoneyArr;
    var moneyStr = this.data.moneyStr;
    var maFeeStr = this.data.maFeeStr;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetBusinessPrestoreQuotaAndReward&UID=" + uid),
      success: function(res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        preMoneyArr = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < preMoneyArr.length; i++) {
          if (Number(preMoneyArr[i].ComeMaFee) < 6) {
            moneyStr += preMoneyArr[i].Money + "，";
            maFeeStr += preMoneyArr[i].ComeMaFee + "%，";
          }
        }
        moneyStr = moneyStr.substring(0, moneyStr.length - 1);
        maFeeStr = maFeeStr.substring(0, maFeeStr.length - 1);
        that.setData({
          preMoneyArr: preMoneyArr,
          moneyStr: moneyStr,
          maFeeStr: maFeeStr,
        })
      }
    })
  },
  // 
  preTap: function(e) {
    this.setData({
      preMoney: e.currentTarget.dataset.premoney,
      rewardMoney: e.currentTarget.dataset.reward,
      comeMaFee: e.currentTarget.dataset.comemafee,
    })
    this.GeneratePrestoreOrder();
  },
  // 
  GeneratePrestoreOrder: function() {
    wx.showLoading({
      title: '订单生成中...',
    })
    var that = this;
    var uid = this.data.uid;
    var preMoney = this.data.preMoney;
    var rewardMoney = this.data.rewardMoney;
    var orderNumber = this.data.orderNumber;
    var comeMaFee = this.data.comeMaFee;
    console.log("method=GeneratePrestoreOrder&UID=" + uid + "&PreMoney=" + preMoney + "&RewardMoney=" + rewardMoney + "&ComeMaFee=" + comeMaFee);
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GeneratePrestoreOrder&UID=" + uid + "&PreMoney=" + preMoney + "&RewardMoney=" + rewardMoney + "&ComeMaFee=" + comeMaFee),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1){
          orderNumber = json.ResultData;
          that.setData({
            orderNumber: orderNumber,
            payFlag: false,
          })
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }else{
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
       
        wx.hideLoading();
      }
    })
  },
  // 
  preInputTap: function() {
    this.setData({
      preInputFlag: false
    })
  },
  // 
  preMoneyInput: function(e) {
    this.setData({
      preMoney: e.detail.value,
    })
  },
  // 
  preInputCancel: function() {
    this.setData({
      preInputFlag: true
    })
  },
  preInputConfirm: function() {
    var that = this;
    var preMoneyArr = this.data.preMoneyArr;
    var preMoney = this.data.preMoney;
    var rewardMoney = this.data.rewardMoney;
    var comeMaFee = this.data.comeMaFee;
    if (preMoney == '') {
      that.setData({
        preInputFlag: true
      })
      return false;
    }
    if (Number(preMoney) >= Number(preMoneyArr[preMoneyArr.length - 1].Money)) {
      rewardMoney = preMoneyArr[preMoneyArr.length - 1].Give;
      comeMaFee = preMoneyArr[preMoneyArr.length - 1].ComeMaFee;
      that.setData({
        preInputFlag: true,
        rewardMoney: rewardMoney,
        comeMaFee: comeMaFee,
      })
      that.GeneratePrestoreOrder();
    } else {
      for (var i = 1; i < preMoneyArr.length; i++) {
        if (Number(preMoney) < Number(preMoneyArr[i].Money)) {
          rewardMoney = preMoneyArr[i - 1].Give;
          comeMaFee = preMoneyArr[i - 1].ComeMaFee;
          that.setData({
            preInputFlag: true,
            rewardMoney: rewardMoney,
            comeMaFee: comeMaFee,
          })
          that.GeneratePrestoreOrder();
          return;
        }
      }
    }

  },
  // 
  //显示支付密码输入层
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function() {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function(e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function() {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=PayPrestoreOrder&OrderNumber=" + orderNumber + "&PayType=0"),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                setTimeout(function() {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 500)
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 
  // 取消付款
  cancelPay: function() {
    this.setData({
      payFlag: true
    })
  },
  //余额或微信 
  radioChange: function(e) {
    var payType = this.data.payType;
    payType = e.detail.value
    this.setData({
      payType: payType,
    });
  },
  // 付款
  goPay: function() {
    var that = this;
    var uid = this.data.uid;
    var payType = this.data.payType;
    var orderNumber = this.data.orderNumber;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (payType == "") {
      wx.showToast({
        title: "请选择支付方式",
        icon: 'none',
        duration: 2000
      });
    }
    if (payType == '0') {
      if (isAlreadyPayPassword == '1') {
        that.setData({
          showPayPwdInput: true,
          payFocus: true,
          payFlag: true
        })
      } else {
        wx.showToast({
          title: "请先去设置支付密码！",
          icon: 'none',
          duration: 2000
        });
      }
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=PayPrestoreOrder&OrderNumber=" + orderNumber + "&PayType=2"),
        success: function(res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          console.log(json);
          if (json.ResultFlag == 1) {
            // wx.requestPayment({
            //   timeStamp: json.ResultData.timestamp,
            //   nonceStr: json.ResultData.nonce_str,
            //   package: 'prepay_id=' + json.ResultData.prepay_id,
            //   signType: 'MD5',
            //   paySign: json.ResultData.sign,
            //   success(res) {
            //     wx.showToast({
            //       title: json.ResultMsg,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //     setTimeout(function() {
            //       wx.navigateBack({
            //         delta: 1
            //       })
            //     }, 500)
            //   },
            //   fail(res) {
            //     console.log(res);
            //     wx.showToast({
            //       title: res.err_desc,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //   }
            // })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }


        }
      })
    }

  },
  // 
})